package com.qd.pay.task;

import com.cdqidi.util.DateUtil;
import com.cdqidi.util.SpringContextHolder;
import com.qd.common.panda.config.PandaParkConfigDTO;
import com.qd.pay.service.TfBillService;
import com.qd.tfpay.order.AccountBO;
import com.qd.tfpay.order.OrderApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author sjk
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class BillTask {
    private final TfBillService tfBillService;
    private final OrderApi orderApi;
    private final PandaParkConfigDTO config;


    @Scheduled(cron = "0 0 1 * * ?")
    public void downloadTf() {
        log.info("downloadTf: {}", SpringContextHolder.getActiveProfile());
        if (SpringContextHolder.getActiveProfile().equals(SpringContextHolder.PROFILE_OFFICIAL)) {
            List<String> partnerList = config.getPartnerList();
            if (null != partnerList && !partnerList.isEmpty()) {
                String s = DateUtil.localDateToStr(LocalDate.now().plusDays(-1));
                for (String partner : partnerList) {
                    log.info("天府银行生成对账文件,商户号: {}, 时间: {}", partner, s);
                    AccountBO accountBO = orderApi.downloadAccount(partner);
                    log.info("天府银行生成对账文件,商户号:{}, 返回: {}", partner, accountBO.toString());
                    accountBO.freeData();
                }
            }
        }
    }

    @Scheduled(cron = "0 30 1 * * ?")
    public void downloadTf2() throws IOException {
        log.info("downloadTf2: {}", SpringContextHolder.getActiveProfile());
        if (SpringContextHolder.getActiveProfile().equals(SpringContextHolder.PROFILE_OFFICIAL)) {
            List<String> partnerList = config.getPartnerList();
            if (null != partnerList && !partnerList.isEmpty()) {
                final String date =  DateUtil.localDateToString(LocalDate.now().plusDays(-1), "yyyyMMdd");
                for (String partner : partnerList) {
                    log.info("天府银行生成下载文件，商户号: {},时间: {}", partner, date);
                    tfBillService. downloadBill(date, partner);
                }
            }

        }
    }
}
